import axios from 'axios'
import qs from 'qs'
// 创建axios实例

const service = axios.create({
  baseURL: '/api', // api的base_url
  timeout: 30000, // 请求超时时间
  withCredentials:true
})
// axios.defaults.withCredentials=true
// request拦截器
service.interceptors.request.use(function (config) {
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
    if(config.method === 'post') {
        config.data = qs.stringify( {
            ...config.data
        })
    }
    return config;
  }, function (error) {
    loadinginstace.close()
    return Promise.reject(error);
  })

// respone拦截器
service.interceptors.response.use(
  response =>{
    if(response.data.code==='T'){
      //404请求接口不存在
       return response.data.data;
    }
    if(response.data.code==='F'){
      return Promise.reject('error');
    }
  },
  error => {
    console.log('err' + error)// for debug
    return Promise.reject(error)
  })

export default service